<template>
  <div class="person">
    <el-card>
      <div slot="header" class="admininfo">
        <span>管理员信息</span>
      </div>
      <div class="main">
        <div class="id">
          <span>管理员ID:{{ form.id }}</span>
        </div>
        <div class="account">
          <span>账号:{{ form.account }}</span>
        </div>
        <div class="userGroup">
          <span>用户组:{{ form.userGroup }}</span>
        </div>
        <div class="creationtime">
          <span>创建时间:{{ form.ctime | normalizeYmd }}</span>
        </div>
      </div>
      <div class="upload">
        <div>管理员头像:</div>
        <el-upload
          class="avatar-uploader"
          :action="Api + '/users/avatar_upload'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.imgUrl" :src="form.imgUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
      <p>
        <el-button type="primary" size="mini" @click="uploader"
          >确定上传</el-button
        >
      </p>
    </el-card>
  </div>
</template>

<script>
import { modifyAvatar } from '@/api/user.js'
export default {
  data () {
    return {
      Api: 'http://127.0.0.1:5000',
      form: {
        id: '',
        ctime: '',
        account: '',
        userGroup: '',
        imgUrl: ''
      },
    }
  },
  methods: {
    getData () {
      this.form = JSON.parse(window.localStorage.getItem('userInfo'))
    },
    //文件上传成功时的钩子
    handleAvatarSuccess (res, file) {//res 就是后台响应的数据 
      let { code, msg, imgUrl } = res
      if (code == 0) {
        this.form.imgUrl = this.Api + imgUrl;
        // console.log('URL.createObjectURL(file.raw) :>> ', this.Api+imgUrl)
      }
    },
    //上传之前触发函数， 作用是限制图片大小和格式
    beforeAvatarUpload (file) {
      const isJPG = /[jpeg|png]/.test(file.type);
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    async uploader () {
      //反向
      let imgUrl = this.form.imgUrl.substr(this.form.imgUrl.lastIndexOf('/') + 1)
      let res = await modifyAvatar({ imgUrl })
      let { code, msg } = res.data
      if (code == 0) {
        this.$bus.$emit('update')
      }
    }
  },
  //生命周期钩子函数 
  created () {
    this.getData()
  }
}
</script>

<style lang="less" scoped>
.main {
  .id,
  .account,
  .userGroup,
  .creationtime {
    line-height: 70px;
    border-bottom: 1px solid #eee;
  }
}
.upload {
  display: flex;
  align-items: center;
  height: 200px;
}
.avatar-uploader /deep/.el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>